﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Repository.MiniSite;
using System.Collections.Generic;
using System.Linq;
using System;
using Utility;
using YunSpace.ThirdService.GetUI;
#endregion

namespace YunSpace.Application.MiniSite
{
    public class CustomerApp
    {
        private ICustomerRepository service = new CustomerRepository();
        private IThirdLoginInfoRepository thirdService = new ThirdLoginInfoRepository();

        public List<CustomerEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<CustomerEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public CustomerEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(CustomerEntity customerEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                customerEntity.Modify(keyValue);
                service.Update(customerEntity);
            }
            else
            {
                var encrypt = new Utility.HashEncrypt();
                var md5Password = encrypt.MD5System(customerEntity.F_Password);

                customerEntity.Create();

                customerEntity.F_Notice = true;
                customerEntity.F_Password = md5Password;
                customerEntity.F_NickName = customerEntity.F_UserName;

                service.Insert(customerEntity);
            }
        }

        /// <summary>
        /// 判断手机是否存在
        /// </summary>
        public bool IsExists(CustomerEntity customerEntity)
        {
            if (customerEntity.IsEmpty()) return false;
            if (customerEntity.F_Phone.IsEmpty()) return false;

            var result = service.FindEntity(p => p.F_Phone == customerEntity.F_Phone);

            return result != null;
        }

        /// <summary>
        /// 验证手机号和密码是否正确
        /// </summary>
        public bool ValidPhoneAndPassword(ref CustomerEntity customerEntity) {
            if (customerEntity.IsEmpty()) return false;
            if (customerEntity.F_Phone.IsEmpty() || customerEntity.F_Password.IsEmpty()) return false;

            var encrypt = new Utility.HashEncrypt();
            var md5Password = encrypt.MD5System(customerEntity.F_Password);
            var phone = customerEntity.F_Phone;

            var result = service.FindEntity(p => p.F_Phone == phone && p.F_Password == md5Password);

            if (result != null)
            {
                customerEntity = result;
            }
            return result != null;
        }

        /// <summary>
        /// 根据第三方用户登录信息获取客户信息
        /// </summary>
        public CustomerEntity GetCustomerByThirdLoginInfo(ThirdLoginInfoEntity thirdLoginInfoEntity) {
            if (thirdLoginInfoEntity.IsEmpty()) return default(CustomerEntity);

            var thirdEntity = thirdService.FindEntity(p => p.F_GUID == thirdLoginInfoEntity.F_GUID && p.F_TypeId == thirdLoginInfoEntity.F_TypeId);
            if (thirdEntity.IsEmpty()) return default(CustomerEntity);

            return service.FindEntity(thirdEntity.F_CustomerId);
        }

        /// <summary>
        /// 根据手机号码获取用户相关信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public CustomerEntity GetCustomerByPhone(string phone) {
            return service.FindEntity(p => p.F_Phone == phone);
        }

        /// <summary>
        /// 用户生成随机密码
        /// </summary>
        public string RandomPassowrd(string phone)
        {
            var rndPassword = Tools.GetValidateCode();
            var entity = service.FindEntity(p => p.F_Phone == phone);

            if (entity.IsEmpty())
            {
                return string.Empty;
            }
            else
            {
                var encrypt = new HashEncrypt();
                var md5Password = encrypt.MD5System(rndPassword);

                entity.F_Password = md5Password;
                service.Update(entity);

                return rndPassword;
            }
        }

        /// <summary>
        /// 禁用个人通知
        /// </summary>
        public void DisabledNotice(string keyValue) {
            var entity = service.FindEntity(keyValue);

            entity.F_Notice = false;
            entity.Modify(keyValue);

            service.Update(entity);

        }

        /// <summary>
        /// 更新个推信息编号
        /// </summary>
        /// <param name="customerId">客户编号</param>
        /// <param name="getUiId">个推编号</param>
        public bool CreateOrUpdateGetUIId(string customerId, string getUiId) {
            return service.CreateOrUpdateGetUIId(customerId, getUiId);
        }

        /// <summary>
        /// 发送个推消息
        /// </summary>
        /// <param name="customerId">客户编号</param>
        /// <param name="content">个推内容</param>
        public bool SendGetUIMessage(string customerId, string content = "") {
            var result = false;
            var entity = service.FindEntity(customerId);
            if (!entity.IsEmpty())
            {
                if (entity.F_Notice.HasValue)
                {
                    if (entity.F_Notice.Value && !entity.F_GetUIId.IsNullOrEmpty())
                    {
                        var data = GetUISendManger.PushMessageToSingle(entity.F_GetUIId, content);
                        // 不用处理个推的数据
                        result = true;
                    }
                }
            }

            return result;
        }

    }
}

